// @flow
import React, { Component } from 'react'
import styles from './JobListActions.css'
import { Button, notification, Popconfirm, message, Modal } from 'antd'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
type Props = {
  history: Object,
  refreshJobList: Function,
  data: Object
}
type State = {
  fileInterface: string
}
class JobListActions extends Component {
  props: Props
  state: State
  sendMsg: Function
  fillShow: Function
  apiShow: Function
  deleteJob: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      fileInterface: 'disabled'
    }
    this.sendMsg = this.sendMsg.bind(this)
    this.deleteJob = this.deleteJob.bind(this)
    this.fillShow = this.fillShow.bind(this)
    this.apiShow = this.apiShow.bind(this)
  }
  componentWillMount () {
    if (this.props.data.resultStatus === 'FAILED') {
      this.setState({
        fileInterface: 'disabled'
      })
    } else if (this.props.data.hasApi === 'false') {
      if (this.props.data.status === 'RUNNING') {
        this.setState({
          fileInterface: 'disabled'
        })
      }
    } else {
      this.setState({
        fileInterface: ''
      })
    }
  }

  deleteJob () {
    const { data } = this.props
    universalFetch(`${__SERVICE_API__}analysis/jobs?jobId=${data.jobId}&projId=${data.modelId}`, {
      method: 'DELETE'
    })
    .then((res) => res.json())
    .then((json) => {
      if (json.code !== 200000) {
        throw new Error(JSON.stringify({ code: json.code, message: json.message }))
      }
      this.props.refreshJobList()
      message.success('删除成功')
    })
     .catch((error) => {
       message.error('删除失败')
       handleFetchError(error)
       console.error(error)
     })
  }

  sendMsg () {
    notification['success']({
      message: '提醒',
      description: '正在推送消息...'
    })
    const { data } = this.props
    let url = `${__SERVICE_API__}analysis/jobs/` + data.jobId + '?status=' + data.resultStatus
    universalFetch(url,
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json;charset=utf-8',
          'Access-Control-Allow-Origin': '*'
        }
      })
       .then((res) => res.json())
       .then((json) => {
         if (json.code === 200000) {
           notification.open({
             message: '提醒',
             description: '消息推送成功!'
           })
         } else {
           notification.error({
             message: '提醒',
             description: json.message,
             duration: 7
           })
           throw new Error(JSON.stringify({ code: json.code, message: json.message }))
         }
       })
       .catch((error) => {
         handleFetchError(error)
         console.error(error)
       })
  }
  fillShow () {
    const jobId = this.props.data.jobId
    let file = `${__SERVICE_API__}analysis/results?jobId=${jobId}&type=file`
    const { history } = this.props
    universalFetch(file)
    .then((res) => res.json())
    .then((json) => {
      if (json.code !== 200000) {
        throw new Error(JSON.stringify({ code: json.code, message: json.message }))
      }
      history.push({
        pathname: '/analysis/job-file',
        state: {
          rowData:json.result
        } })
    })
    .catch((error) => {
      handleFetchError(error)
      console.error(error)
    })
  }
  apiShow () {
    const jobId = this.props.data.jobId
    let file = `${__SERVICE_API__}analysis/results?jobId=${jobId}&type=interface`
    universalFetch(file)
    .then((res) => res.json())
    .then((json) => {
      if (json.code !== 200000) {
        notification.error({
          message: '提醒',
          description: json.message,
          placement: 'topRight',
          top: 63
        })
        throw new Error(JSON.stringify({ code: json.code, message: json.message }))
      }
      Modal.info({
        title: '接口内容：',
        content: JSON.stringify(json),
        okText:'关闭',
        onOk () {
          // console.log('OK')
        },
        onCancel () {
          // console.log('Cancel')
        }
      })
      // notification.open({
      //   message: ' ',
      //   description: JSON.stringify(json),
      //   duration: null
      // })
    })
    .catch((error) => {
      handleFetchError(error)
      console.error(error)
    })
  }
  render () {
    return (
      <div className={styles['actions']}>
        <Button
          size='small'
          type='primary'
          icon='file'
          style={{ marginRight: '2px' }}
          onClick={this.fillShow}
          disabled={this.state.fileInterface}
          >
          文件
        </Button>
        <Button
          size='small'
          type='primary'
          style={{ marginRight: '10px' }}
          onClick={this.apiShow}
          disabled={this.state.fileInterface}
          >
          接口
        </Button>
        <Popconfirm title='确定要删除此作业吗'
          arrowPointAtCenter
          placement='left'
          onConfirm={this.deleteJob}
          >
          <Button
            size='small'
            type='danger'
            style={{ marginRight: '10px' }}
            >
            删除
          </Button>
        </Popconfirm>
      </div>
    )
  }
}

export default JobListActions
